<?php

namespace app\controllers;

use Yii;
use app\tools\Re;
use app\models\Ad;
use app\models\Article;

class HomeController extends BaseController
{
  //首页数据接口
  public function actionIndex(){
    $article_recom_list = Article::find()
      ->where(['is_recom' => 1, 'status' => 1])
      ->andWhere('cate_id <> 6')
      ->orderBy("created_at desc")
      ->limit(1)
      ->all();
    $article_recom_data = [];
    if ($article_recom_list) {
      foreach ($article_recom_list as $v) {
        $article_recom_data[] = [
          'id' => $v->id,
          'title' => $v->title,
          'content' => $this->getContent($v->content)
        ];
      }
    }

    $article_list = Article::find()
      ->where(['article_recom' => 1, 'status' => 1])
      ->andWhere('cate_id <> 6')
      ->orderBy("created_at desc")
      ->all();
    $article_data = [];
    if ($article_list) {
      foreach ($article_list as $v) {
        $article_data[] = [
          'id' => $v->id,
          'title' => $v->title,
          'cover' => $v->cover,
          'source' => $v->source,
          'created_time' => date("Y-m-d", $v->created_at),
        ];
      }
    }

    $ad_lb_list = Ad::find()
      ->where(['type' => 1, 'status' => 1])
      ->orderBy("created_at desc")
      ->limit(5)
      ->all();
    $ad_lb_data = [];
    if ($ad_lb_list) {
      foreach ($ad_lb_list as $v) {
        $ad_lb_data[] = [
          'id' => $v->id,
          'cover' => $v->cover,
          'target' => $v->target,
        ];
      }
    }

    $ad_video = Ad::find()->where(['type' => 2, 'status' => 1])->select('cover,video_url,target')->orderBy("created_at desc")->one();

    $ad_img = Ad::find()->where(['type' => 3, 'status' => 1])->select('cover,target')->orderBy("created_at desc")->one();

    $data = [
      'article_list_recom' => $article_recom_data,
      'ad_list_lb' => $ad_lb_data,
      'ad_video' => $ad_video ?: (object) array(),
      'ad_img' => $ad_img ?:  (object) array(),
      'article_list' => $article_data,
    ];
    return Re::data(['data' => $data]);
  }

  private function getContent($content) {
    $content= str_replace("&nbsp;", "",$content);
    $content = strip_tags($content);
    $content = trim($content);
    return mb_substr($content, 0, 20, 'utf-8');
  }

  //资讯列表
  public function actionArticleList(){
    $page = Yii::$app->request->post('page', 0);
    $cate_id = Yii::$app->request->post('cate_id', 0);
    $title = Yii::$app->request->post('title', '');
    $article_query = Article::find()->where(['status' => 1])->andWhere('cate_id <> 6');
    if ($cate_id) {
      $article_query->andWhere(['cate_id' => $cate_id]);
    }
    if ($title) {
      $article_query->andWhere(['like', 'title', $title]);
    }
    $article_list = $article_query->orderBy("created_at desc")->limit(10)->offset(10*$page)->all();
    $article_data = [];
    if ($article_list) {
      foreach ($article_list as $v) {
        $article_data[] = [
          'id' => $v->id,
          'title' => $v->title,
          'cover' => $v->cover,
          'source' => $v->source,
          'created_time' => date("Y-m-d", $v->created_at),
        ];
      }
    }
    return Re::data(['data' => $article_data]);
  }

  //资讯详情
  public function actionArticleDetail(){
    $id = Yii::$app->request->post('id', 0);
    $article = Article::findOne($id);
    if (!$article) {
      return Re::data(null, '资讯信息读取失败', Re::ERROR);
    }
    $data = [
      'id' => $article->id,
      'title' => $article->title,
      'cover' => $article->cover,
      'content' => $article->content,
      //'content' => '',
      'source' => $article->source,
      'created_time' => date("Y-m-d", $article->created_at),
    ];
    return Re::data(['data' => $data]);
  }
}
